package JavaDesignModel;
// 实验二  使用单一职责原则，开闭原则，依赖倒转原则实现一个简单的图书管理系统

import java.util.*;

class Main {
    public static void main(String[] args) {
        // 创建依赖对象
        BookRepository repository = new BookRepository();
        BookExporter consoleExporter = new ConsoleExporter();
        BookExporter xmlExporter = new XmlExporter();
        BookExporter jsonExporter = new JsonExporter(); // 新增JSON导出器

        // 创建服务实例（依赖注入）
        BookService consoleService = new BookService(repository, consoleExporter);
        BookService xmlService = new BookService(repository, xmlExporter);
        BookService jsonService = new BookService(repository, jsonExporter); // 新增JSON服务

        Scanner scanner = new Scanner(System.in);
        // 循环添加书籍
        for (int i = 0; i < 2; i++) {
            System.out.println("\n添加第 " + (i + 1) + " 本书:");

            System.out.print("请输入书名: ");
            String title = scanner.nextLine();

            System.out.print("请输入作者: ");
            String author = scanner.nextLine();

            System.out.print("请输入价格: ");
            double price = scanner.nextDouble();
            scanner.next();

            Book book = new Book(title, author, price);
            consoleService.addBook(book);
        }

        // 导出到控制台
        System.out.println("\n--- 调用控制台输出展示 ---");
        consoleService.exportBooks();

        // 导出到XML
        System.out.println("\n--- XML导出功能展示 ---");
        xmlService.exportBooks();

        // 新增JSON导出演示
        System.out.println("\n--- JSON导出功能展示 ---");
        jsonService.exportBooks();
    }
}

// 新增的JsonExporter类
class JsonExporter implements BookExporter {
    @Override
    public void export(List<Book> books) {
        System.out.println("Exporting to JSON:");
        System.out.println("[");
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            System.out.println("  {");
            System.out.println("    \"title\": \"" + book.getTitle() + "\",");
            System.out.println("    \"author\": \"" + book.getAuthor() + "\",");
            System.out.println("    \"price\": " + book.getPrice());
            System.out.print("  }");
            if (i < books.size() - 1) {
                System.out.println(",");
            } else {
                System.out.println();
            }
        }
        System.out.println("]");
    }
}

class Book {
    private String title;
    private String author;
    private double price;

    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }
}

class BookRepository {
    private List<Book> books = new ArrayList<>();

    public void addBook(Book book) {
        books.add(book);
    }

    public List<Book> getAllBooks() {
        return new ArrayList<>(books); // 返回副本以保护内部数据
    }
}

interface BookExporter {
    void export(List<Book> books);
}

class ConsoleExporter implements BookExporter {
    @Override
    public void export(List<Book> books) {
        System.out.println("Books in Console:");
        for (Book book : books) {
            System.out.println("Title: " + book.getTitle() +
                    ", Author: " + book.getAuthor() +
                    ", Price: " + book.getPrice());
        }
    }
}

class XmlExporter implements BookExporter {
    @Override
    public void export(List<Book> books) {
        System.out.println("Exporting to XML:");
        System.out.println("<books>");
        for (Book book : books) {
            System.out.println("  <book>");
            System.out.println("    <title>" + book.getTitle() + "</title>");
            System.out.println("    <author>" + book.getAuthor() + "</author>");
            System.out.println("    <price>" + book.getPrice() + "</price>");
            System.out.println("  </book>");
        }
        System.out.println("</books>");
    }
}

public class BookService {
    private final BookRepository bookRepository;
    private final BookExporter bookExporter;

    // 依赖注入通过构造函数
    public BookService(BookRepository bookRepository, BookExporter bookExporter) {
        this.bookRepository = bookRepository;
        this.bookExporter = bookExporter;
    }

    public void addBook(Book book) {
        bookRepository.addBook(book);
    }

    public void exportBooks() {
        List<Book> books = bookRepository.getAllBooks();
        bookExporter.export(books);
    }
}